﻿using Entidades;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace AccesoDatos
{
    public class ADCliente
    {

        Database oDatabase = DatabaseFactory.CreateDatabase(ConfigurationManager.AppSettings["conexionBD"]);

        //public List<ENCliente> ClientesListar(int pagina, int cantRegxPag,string busqueda)
        public List<ENCliente> ClientesListar(string busqueda)
        {
            DbCommand oDbCommand = oDatabase.GetStoredProcCommand("USP_SEL_CLIENTES");
            oDatabase.AddInParameter(oDbCommand, "nombre", DbType.String, busqueda);
            //oDatabase.AddInParameter(oDbCommand, "pagina", DbType.Int32, pagina);
            //oDatabase.AddInParameter(oDbCommand, "cantRegxPag", DbType.Int32, cantRegxPag);
            List<ENCliente> lista = new List<ENCliente>();

            using (IDataReader reader = oDatabase.ExecuteReader(oDbCommand))
            {
                while (reader.Read())
                    lista.Add(new ENCliente(reader));
            }
            return lista;
        }

        public List<ENCliente> ClientesListarPag(
          int pagina, int cantRegxPag)
        {
            DbCommand odbcommand = oDatabase.GetStoredProcCommand("USP_SEL_CLIENTES_PAG");
            oDatabase.AddInParameter(odbcommand, "pagina", DbType.Int32, pagina);
            oDatabase.AddInParameter(odbcommand, "cantRegxPag", DbType.Int32, cantRegxPag);
            List<ENCliente> lista = new List<ENCliente>();
            using (IDataReader reader = oDatabase.ExecuteReader(odbcommand))
            {
                while (reader.Read())
                    lista.Add(new ENCliente(reader));
            }
            return lista;
        }

        public ENCliente ClientexContrato(int Cliente_Id)
        {
            DbCommand oDbCommand = oDatabase.GetStoredProcCommand("usp_Cliente_select_contrato", Cliente_Id);
            ENCliente cliente = new ENCliente();

            using (IDataReader reader = oDatabase.ExecuteReader(oDbCommand))
            {
                if (reader.Read())
                    cliente = new ENCliente(reader);
            }
            return cliente;
        }

        //ClientesListarTotal
        public List<ENCliente> ClientesListarTotal()
        {
            DbCommand oDbCommand = oDatabase.GetStoredProcCommand("USP_SEL_CLIENTES_TOT");
            List<ENCliente> lista = new List<ENCliente>();

            using (IDataReader reader = oDatabase.ExecuteReader(oDbCommand))
            {
                while (reader.Read())
                    lista.Add(new ENCliente(reader));
            }
            return lista;
        }

    }
}
